package apis

import (
	"github.com/gin-gonic/gin"
	"github.com/go-admin-team/go-admin-core/sdk/api"
	"time"
	"tuowei.com/app/admin/models"
	"tuowei.com/app/admin/service"
	"tuowei.com/app/admin/service/dto"
	"tuowei.com/common/actions"
)

type ApiLiveCourse struct {
	api.Api
}

func (e ApiLiveCourse) GetPages(c *gin.Context) {
	s := service.SysLiveCourse{}
	req := dto.SysLiveCourseGetPageReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}

	//数据权限检查
	p := actions.GetPermissionFromContext(c)

	list := make([]models.SysLiveCourse, 0)
	var count int64

	err = s.GetPage(&req, p, &list, &count)
	if err != nil {
		e.Error(500, err, "查询失败")
		return
	}

	for k, value := range list {
		list[k].StatusText = getStatusText(value.StartTime, value.EndTime)
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func getStatusText(startTime, endTime time.Time) string {

	if time.Now().Unix() > startTime.Unix() && time.Now().Unix() < endTime.Unix() {
		return "直播中"
	} else if time.Now().Unix() < startTime.Unix() {
		return "未开始"
	} else if time.Now().Unix() > endTime.Unix() {
		return "已结束"
	}
	return ""
}
